package com.egao.texasHoldEmPoker.biz.impl;

import com.egao.common.core.damon.constant.UserConstant;
import com.egao.common.core.damon.results.BaseDao;
import com.egao.common.core.damon.results.PageResult2;
import com.egao.common.core.damon.utils.Utils;
import com.egao.common.system.entity.User;
import com.egao.texasHoldEmPoker.biz.PlayerBiz;
import com.egao.texasHoldEmPoker.entity.Player;
import com.egao.common.core.web.JsonResult;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.dao.EmptyResultDataAccessException;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.RowMapper;
import org.springframework.stereotype.Repository;

import java.util.HashMap;
import java.util.Map;
import java.util.List;

/**
 *
 * 数据层实现类
 * @author : Doman
 * @date : 2022-01-26 12:39:41
 * @version ：1.0
 */
@Repository
public class PlayerBizImpl extends BaseDao implements PlayerBiz {

	@Override
	public JsonResult list(PageResult2<T> page, Player player, User loginUser) throws Exception {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Player> rm = new BeanPropertyRowMapper<Player>(Player.class);
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.desk_id");
    sql.append(",p.wechat_code,p.wechat_name,p.poker5,p.poker6");
		sql.append(",p.state,p.fold,p.indebted,p.face_value,p.win_price");
		sql.append(",p.remark,p.order_num,p.create_time,p.create_user");
		sql.append(",p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(player)){
//			if(Utils.isNotEmpty(player.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", player.getName());
//			}
    if(Utils.isNotEmpty(player.getSort())&&Utils.isNotEmpty(player.getOrder())){
        sql.append(" order by p."+player.getSort()+" "+player.getOrder());
      } else {
        sql.append(" order by p.create_time desc");
      }
		}
		return jsonResult(getPageQueryList(page, sql.toString(), map, rm));
	}

	@Override
	public List<Player> list(Player player, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		RowMapper<Player> rm = new BeanPropertyRowMapper<Player>(Player.class);
    StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.desk_id");
    sql.append(",p.wechat_code,p.wechat_name,p.poker5,p.poker6");
		sql.append(",p.state,p.fold,p.indebted,p.face_value,p.win_price");
		sql.append(",p.remark,p.order_num,p.create_time,p.create_user");
		sql.append(",p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 ");
		if(loginUser.getUserId() != UserConstant.ADMIN_ID){
      sql.append(" and p.organization_id=:tableOrganizationId ");
      map.put("tableOrganizationId", loginUser.getOrganizationId() + "");
    }
    if(Utils.isNotEmpty(player)){
//			if(Utils.isNotEmpty(player.getName())){
//				sql.append(" and instr(p.name,:name)>0 ");
//				map.put("name", player.getName());
//			}
      if(Utils.isNotEmpty(player.getSort())&&Utils.isNotEmpty(player.getOrder())){
				sql.append(" order by p."+player.getSort()+" "+player.getOrder());
			} else {
				sql.append(" order by p.create_time desc");
			}
		}
		getMapParameter(sql, map);
		return jdbcTemplate.query(sql.toString(),map, rm);
	}

	@Override
	public Player getById(Player player) {
		RowMapper<Player> rm = new BeanPropertyRowMapper<Player>(Player.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.desk_id");
    sql.append(",p.wechat_code,p.wechat_name,p.poker5,p.poker6");
		sql.append(",p.state,p.fold,p.indebted,p.face_value,p.win_price");
		sql.append(",p.remark,p.order_num,p.create_time,p.create_user");
		sql.append(",p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 and p.id=:id ");
		map.put("id", player.getId());
		getMapParameter(sql, map);
		return jdbcTemplate.queryForObject(sql.toString(), map, rm);
	}

	@Override
	public Player getByCodeAndDesk(Player player) {
		RowMapper<Player> rm = new BeanPropertyRowMapper<Player>(Player.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.desk_id");
    sql.append(",p.wechat_code,p.wechat_name,p.poker5,p.poker6");
		sql.append(",p.state,p.fold,p.indebted,p.face_value,p.win_price");
		sql.append(",p.remark,p.order_num,p.create_time,p.create_user");
		sql.append(",p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 and p.desk_id=:desk_id and p.wechat_code=:wechat_code ");
		map.put("desk_id", player.getDeskId()+"");
		map.put("wechat_code", player.getWechatCode());
		getMapParameter(sql, map);
		try {
			return jdbcTemplate.queryForObject(sql.toString(), map, rm);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public List<Player> getByDesk(Player player) {
		RowMapper<Player> rm = new BeanPropertyRowMapper<Player>(Player.class);
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql = new StringBuffer("select p.id,p.organization_id,p.desk_id");
    sql.append(",p.wechat_code,p.wechat_name,p.poker5,p.poker6");
		sql.append(",p.state,p.fold,p.indebted,p.face_value,p.win_price");
		sql.append(",p.remark,p.order_num,p.create_time,p.create_user");
		sql.append(",p.update_time,p.update_user");
    sql.append(",sogzn.organization_name organizationName,suser.username createName ");
    sql.append(" from th_player p ");
    sql.append(" inner join sys_user suser on suser.user_id=p.create_user ");
    sql.append(" inner join sys_organization sogzn on sogzn.organization_id=p.organization_id and sogzn.deleted=0 ");
    sql.append(" where p.deleted=0 and p.desk_id=:desk_id ");
		map.put("desk_id", player.getDeskId()+"");
		sql.append(" order by p.order_num asc ");
		getMapParameter(sql, map);
		try {
			return jdbcTemplate.query(sql.toString(), map, rm);
		} catch (EmptyResultDataAccessException e) {
			return null;
		}
	}

	@Override
	public boolean save(Player player, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("insert into th_player (");
    sql.append("organization_id,desk_id,wechat_code,wechat_name");
		sql.append(",poker5,poker6,state");
		sql.append(",indebted,face_value,win_price,remark,order_num");
		sql.append(",create_user,create_time) values ( ");
    sql.append(":organization_id,:desk_id,:wechat_code,:wechat_name");
		sql.append(",:poker5,:poker6,:state");
		sql.append(",:indebted,:face_value,:win_price,:remark,:order_num");
		sql.append(",:create_user,now())");
    map.put("organization_id", loginUser.getOrganizationId() + "");
    map.put("desk_id", player.getDeskId()==null?null:player.getDeskId()+"");
    map.put("wechat_code", player.getWechatCode());
    map.put("wechat_name", player.getWechatName());
    map.put("poker5", player.getPoker5());
    map.put("poker6", player.getPoker6());
    map.put("state", player.getState()==null?null:player.getState()+"");
    map.put("indebted", player.getIndebted() + "");
    map.put("face_value", player.getFaceValue() + "");
    map.put("win_price", player.getWinPrice() + "");
    map.put("remark", player.getRemark());
    map.put("order_num", player.getOrderNum()==null?null:player.getOrderNum()+"");
		map.put("create_user", loginUser.getUserId() + "");
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean update(Player player, User loginUser) {
		Map<String, String> map  = new HashMap<String, String>();
		StringBuffer sql =new StringBuffer("update th_player set ");
    sql.append("desk_id=:desk_id,wechat_code=:wechat_code,wechat_name=:wechat_name ");
		sql.append(",poker5=:poker5,poker6=:poker6,state=:state,fold=:fold ");
		sql.append(",indebted=:indebted,face_value=:face_value,win_price=:win_price ");
		sql.append(",remark=:remark,order_num=:order_num,update_time=now() ");
		sql.append(",update_user=:update_user where id=:id ");
    map.put("desk_id", player.getDeskId()==null?null:player.getDeskId()+"");
    map.put("wechat_code", player.getWechatCode());
    map.put("wechat_name", player.getWechatName());
    map.put("poker5", player.getPoker5());
    map.put("poker6", player.getPoker6());
    map.put("state", player.getState()==null?null:player.getState()+"");
    map.put("fold", player.getFold()+"");
    map.put("indebted", player.getIndebted() + "");
    map.put("face_value", player.getFaceValue() + "");
    map.put("win_price", player.getWinPrice() + "");
    map.put("remark", player.getRemark());
    map.put("order_num", player.getOrderNum()==null?null:player.getOrderNum()+"");
		map.put("update_user", loginUser.getUserId() + "");
		map.put("id", player.getId());
		getMapParameter(sql, map);
		int count = jdbcTemplate.update(sql.toString(), map);
		return count > 0;
	}

	@Override
	public boolean remove(Player player, User loginUser) {
		int count = 0;
		Map<String, String> map = null;
		if(player.getId().length()>0){
			for(String ID : player.getId().split(",")){
				map  = new HashMap<String, String>();
				if(ID==null || ID.equals("")){ break; }
				StringBuffer sql =new StringBuffer("update th_player set deleted=1" +
						",update_user=:update_user, update_time=now() "
						+ " where id =:id ");
				map.put("update_user", loginUser.getUserId() + "");
				map.put("id", ID);
				getMapParameter(sql, map);
				count = jdbcTemplate.update(sql.toString(),map);
			}
		}
		return count > 0;
	}
}
